package com.example.standard.controller;

import com.example.standard.entity.Standard;
import com.example.standard.service.StandardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

@RestController
@RequestMapping("/api")
public class StandardContrller {
    @Autowired
    StandardService standardService;
//    高级查询
    @GetMapping("/selectAll")
    public Page<Standard> index(@RequestParam(required = false,defaultValue = "") String name, @RequestParam(required = false,defaultValue = "1") Integer pageindex){
        System.out.println("name = " + name);
        return standardService.search("%"+name+"%",pageindex);
    }
//    根据id进入修改页面
    @GetMapping("/update/{id}")
    public Standard update(@PathVariable Integer id){
        System.out.println("id = " + id);
       return standardService.selectID(id);
    }
//    修改
    @PostMapping("/update/{id}")
    @ResponseBody
    public Integer update(String name,String zhname,String version,String keys,String ksdate,String jsdate,String path, Integer id,  @RequestParam("file") MultipartFile file) {
        //1.指定文件上传目录
        Standard standard1 = new Standard();
        System.out.println("name = " + name);
        System.out.println("zhname = " + zhname);
        System.out.println("version = " + version);
        System.out.println("keys = " + keys);
        System.out.println("ksdate = " + ksdate);
        System.out.println("jsdate = " + jsdate);
        System.out.println("id = " + id);
        System.out.println("multipartFile = " + file);
        System.out.println("path = " + path);
        try {
            //1.获取文件名
            String realFilename = file.getOriginalFilename();
            //2.截取文件后缀名
            String imgSuffix = realFilename.substring(realFilename.lastIndexOf("."));
            //3.生成唯一文件名（不能使用中文命名，在网页访问过程中统一英文命名，中文名可能会出现乱码，需要进一步处理）
            String newFilename = UUID.randomUUID().toString() + imgSuffix;
            //4.生成日期目录
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyy/MM/dd");
            String dataPath = dateFormat.format(new Date());
            //5.指定最终目录
            File targetPath = new File("D://shangchuan/" +realFilename);
            if (!targetPath.exists()) targetPath.mkdirs();//不存在目录则创建
            //6.指定文件上传后服务器的完整文件名
            File targetFileName = new File(targetPath, newFilename);
            //7.文件上传
            file.transferTo(targetFileName);

            standard1.setPath(String.valueOf(targetPath));
            standard1.setJsdate(jsdate);
            standard1.setName(name);
            standard1.setZhname(zhname);
            standard1.setVersion(version);
            standard1.setKeys(keys);
            standard1.setKsdate(ksdate);

        } catch (IOException e) {
            e.printStackTrace();
        }
//        String name,String zhname,String version,String keys,String ksdate,String jsdate,String path
        standardService.update(standard1.getName(),standard1.getZhname(),standard1.getVersion(),standard1.getKeys(),standard1.getKsdate(),standard1.getJsdate(),standard1.getPath(),id);
        return 1;
    }
//
    @GetMapping("/checked")
    public int checked(String biaozhunhao){
        return  standardService.count(biaozhunhao);
    }
    @PostMapping("/delete")
    public  int delete(int [] id){
        return  standardService.delete(id);
    }

    @PostMapping("/checked")
    public int checked(String name,String zhname,String version,String keys,String ksdate,String jsdate,@RequestParam("file") MultipartFile file){
        System.out.println("ksdate = " + ksdate);
        System.out.println("jsdate = " + jsdate);
        System.out.println("multipartFile = " + file);

        Standard standard1 = new Standard();
        try {
            //1.获取文件名
            String realFilename = file.getOriginalFilename();
            //2.截取文件后缀名
            String imgSuffix = realFilename.substring(realFilename.lastIndexOf("."));
            //3.生成唯一文件名（不能使用中文命名，在网页访问过程中统一英文命名，中文名可能会出现乱码，需要进一步处理）
            String newFilename = UUID.randomUUID().toString() + imgSuffix;
            //4.生成日期目录
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyy/MM/dd");
            String dataPath = dateFormat.format(new Date());
            //5.指定最终目录
            File targetPath = new File("D://shangchuan/" +realFilename);
            if (!targetPath.exists()) targetPath.mkdirs();//不存在目录则创建
            //6.指定文件上传后服务器的完整文件名
            File targetFileName = new File(targetPath, newFilename);
            //7.文件上传
            file.transferTo(targetFileName);

            standard1.setPath(String.valueOf(targetPath));
            standard1.setKsdate(jsdate);
            standard1.setName(name);
            standard1.setZhname(zhname);
            standard1.setVersion(version);
            standard1.setKeys(keys);
            standard1.setJsdate(ksdate);


        } catch (IOException e) {
            e.printStackTrace();
        }
        standardService.insert(standard1.getName(),standard1.getZhname(),standard1.getVersion(),standard1.getKeys(),standard1.getKsdate(),standard1.getJsdate(),standard1.getPath());
        return 1;
    }


}
